﻿using DevExpress.Utils;
using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        WaitDialogForm waitDlg;
        LoginForm loginDlg;
        bool loggedIn;
        log4net.ILog logger;
        public MainForm(WaitDialogForm waitdlg, log4net.ILog logger)
        {
            InitializeComponent();

            this.waitDlg = waitdlg;
            this.logger = logger;

            LoginInfo.Instance.ComputerName = Global.Instance.GetComputerName().ToUpper();
            LoginInfo.Instance.ComputerIP = Global.Instance.GetComputerIP();
            LoginInfo.Instance.ComputerMAC = Global.Instance.GetComputerMAC();
        }
        private void LoadPermission(List<FunctionModel> functions)
        {
            foreach (FunctionModel func in functions)
            {
                switch (func.Id)
                {
                    case "010101":
                        // 查看员工信息
                        this.bsiBasic.Visibility = BarItemVisibility.Always;
                        this.bbiStaff.Visibility = BarItemVisibility.Always;
                        break;
                    case "010201":
                        // 查看角色信息
                        this.bsiBasic.Visibility = BarItemVisibility.Always;
                        this.bbiRole.Visibility = BarItemVisibility.Always;
                        break;
                    case "010303":
                        // 修改密码
                        this.bsiBasic.Visibility = BarItemVisibility.Always;
                        this.bbiChangePwd.Visibility = BarItemVisibility.Always;
                        break;
                    case "010401":
                        // 查看日志
                        this.bsiBasic.Visibility = BarItemVisibility.Always;
                        this.bbiStaffLog.Visibility = BarItemVisibility.Always;
                        break;
                    case "010501":
                        // 查看城市信息
                        this.bsiBasic.Visibility = BarItemVisibility.Always;
                        this.bbiCity.Visibility = BarItemVisibility.Always;
                        break;
                    case "010601":
                        // 查看航空公司信息
                        this.bsiBasic.Visibility = BarItemVisibility.Always;
                        this.bbiAirline.Visibility = BarItemVisibility.Always;
                        break;
                    case "010701":
                        // 查看证件类型信息
                        this.bsiBasic.Visibility = BarItemVisibility.Always;
                        this.bbiIDType.Visibility = BarItemVisibility.Always;
                        break;
                    case "010801":
                        // 查看国家信息
                        this.bsiBasic.Visibility = BarItemVisibility.Always;
                        this.bbiNation.Visibility = BarItemVisibility.Always;
                        break;

                    case "020101":
                        // 查看客户信息
                        this.bsiCustomer.Visibility = BarItemVisibility.Always;
                        this.bbiCustomerInfo.Visibility = BarItemVisibility.Always;
                        break;

                    case "030101":
                        // 查看预登记信息
                        this.bsiBooking.Visibility = BarItemVisibility.Always;
                        this.bbiPreRegister.Visibility = BarItemVisibility.Always;
                        break;
                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                this.siStaff.Caption = "尚未登录";
                this.siWorkstation.Caption = string.Format("{0}[{1}]", LoginInfo.Instance.ComputerName, LoginInfo.Instance.ComputerIP);
                this.siSystemTime.Caption = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                this.timer1.Start();

                this.loginDlg = new LoginForm();
                this.waitDlg.Close();

                if (this.loginDlg.ShowDialog(this) != DialogResult.OK)
                {
                    this.Close();
                    return;
                }
                this.loggedIn = true;
                this.siStaff.Caption = string.Format("{0}({1})", LoginInfo.Instance.StaffLogon.Staff.Name, LoginInfo.Instance.StaffLogon.Staff.Id);
                this.LoadPermission(LoginInfo.Instance.StaffLogon.Functions);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (this.loggedIn && !Global.Instance.QueryDlg("是否真的要退出本系统？"))
                {
                    e.Cancel = true;
                    return;
                }
                this.loginDlg.Dispose();
                this.waitDlg.Dispose();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.siSystemTime.Caption = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void bbiStaff_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<StaffForm>(this);
        }

        private void bbiRole_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<RoleForm>(this);
        }

        private void bbiChangePwd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (ChangePwdForm dlg = new ChangePwdForm())
            {
                dlg.ShowDialog();
            }
        }

        private void bbiStaffLog_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<StaffLogForm>(this);
        }

        private void bbiCity_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<CityForm>(this);
        }

        private void bbiAirline_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<AirlineForm>(this);
        }

        private void bbiIDType_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<IDTypeForm>(this);
        }

        private void bbiCustomerInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<CustomerForm>(this);
        }

        private void bbiNation_ItemClick(object sender, ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<NationForm>(this);
        }

        private void bbiPreRegister_ItemClick(object sender, ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<PreRegisterForm>(this);
        }

        private void bbiBookingFlight_ItemClick(object sender, ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<BookingForm>(this);
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            Global.Instance.ShowMdiForm<OnlineBooking>(this);
        }
    }
}
